package com.wangshb.upms.core.spring.security;


import com.wangshb.upms.core.common.GpResponse;
import com.wangshb.upms.core.utils.HttpUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Service;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CustomAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request,
                       HttpServletResponse response,
                       AccessDeniedException accessDeniedException) throws IOException,
            ServletException {
        response.setStatus(403);
        HttpUtils.write(response, GpResponse.fail(accessDeniedException.getMessage(), null));
        /*if (HttpUtils.isAjaxRequest(request)) {// AJAX请求,使用response发送403
            response.sendError(403);
        } else if (!response.isCommitted()) {// 非AJAX请求，跳转系统默认的403错误界面，在web.xml中配置
            response.sendError(HttpServletResponse.SC_FORBIDDEN,
                    accessDeniedException.getMessage());
        }*/
    }

}